<template>
  <div class="content-section" :class="{ active: isActive }">
    <div class="agent-container">
      <div class="agent-header">
        <div class="header-content">
          <h2><i class="ai-icon">🤖</i>K8s AI 智能助手</h2>
          <div class="status-indicator" :class="{ online: isOnline }">
            <span class="status-dot"></span>
            {{ isOnline ? "在线" : "离线" }}
          </div>
        </div>
      </div>

      <div class="chat-container">
        <!-- 会话管理区域 -->
        <div class="session-header">
          <div class="session-controls">
            <button
              @click="createNewSession"
              class="control-btn new-session-btn"
            >
              <i class="icon">➕</i>
              <span>新建对话</span>
            </button>

            <div class="session-selector">
              <select
                v-model="currentSessionId"
                @change="switchSession"
                class="session-select"
              >
                <option
                  v-for="session in sessionList"
                  :key="session.sessionId"
                  :value="session.sessionId"
                >
                  {{ getSessionDisplayName(session) }}
                </option>
              </select>
              <div class="session-info" v-if="currentSession">
                <span class="message-count"
                  >{{ currentSession.messageCount || 0 }} 条消息</span
                >
                <span class="session-time">{{
                  formatTime(currentSession.lastUpdated)
                }}</span>
              </div>
            </div>

            <button
              @click="refreshSessions"
              class="control-btn refresh-btn"
              :disabled="isLoading"
            >
              <i class="icon" :class="{ rotating: isLoading }">🔄</i>
            </button>

            <button
              @click="deleteCurrentSession"
              class="control-btn delete-btn"
              :disabled="sessionList.length <= 1 || isLoading"
            >
              <i class="icon">🗑️</i>
            </button>
          </div>
        </div>

        <!-- 消息显示区域 -->
        <div class="chat-messages" ref="agentMessagesRef">
          <div class="welcome-message" v-if="agentMessages.length === 0">
            <div class="welcome-content">
              <div class="welcome-icon">🎉</div>
              <h3>欢迎使用 K8s AI 助手</h3>
              <p>我可以帮助您管理和监控Kubernetes集群</p>
              <div class="suggestions">
                <div
                  class="suggestion-item"
                  @click="sendSuggestion('检查集群健康状况')"
                >
                  🏥 检查集群健康状况
                </div>
                <div
                  class="suggestion-item"
                  @click="sendSuggestion('获取所有命名空间')"
                >
                  📋 获取所有命名空间
                </div>
                <div
                  class="suggestion-item"
                  @click="sendSuggestion('查看Pod状态概览')"
                >
                  📊 查看Pod状态概览
                </div>
              </div>
            </div>
          </div>

          <div
            v-for="(message, index) in agentMessages"
            :key="index"
            :class="['message', message.type]"
          >
            <div class="message-avatar">
              <div v-if="message.type === 'user'" class="avatar user-avatar">
                👤
              </div>
              <div v-else class="avatar assistant-avatar">🤖</div>
            </div>
            <div class="message-bubble">
              <div
                class="message-content"
                v-html="formatMessage(message.content)"
              ></div>

              <!-- 添加反馈按钮 -->
              <div
                v-if="
                  message.feedbackId && pendingFeedbacks.has(message.feedbackId)
                "
                class="feedback-buttons"
              >
                <button
                  @click="submitFeedback(message.feedbackId, true)"
                  class="feedback-btn effective-btn"
                >
                  ✅ 有效
                </button>
                <button
                  @click="submitFeedback(message.feedbackId, false)"
                  class="feedback-btn ineffective-btn"
                >
                  ❌ 无效
                </button>
                <button
                  @click="showFeedbackDialog(message.feedbackId)"
                  class="feedback-btn comment-btn"
                >
                  💬 详细反馈
                </button>
              </div>

              <div class="message-time">
                {{ formatMessageTime(message.timestamp) }}
              </div>
            </div>
          </div>

          <div v-if="isTyping" class="typing-indicator">
            <div class="message assistant">
              <div class="message-avatar">
                <div class="avatar assistant-avatar">🤖</div>
              </div>
              <div class="message-bubble">
                <div class="typing-animation">
                  <span></span>
                  <span></span>
                  <span></span>
                </div>
              </div>
            </div>
          </div>
        </div>

        <!-- 输入区域 -->
        <div class="chat-input">
          <div class="input-container">
            <input
              type="text"
              v-model="agentInput"
              @keyup.enter="sendAgentMessage"
              placeholder="请输入您的问题..."
              :disabled="isLoading"
              class="message-input"
            />
            <button
              @click="sendAgentMessage"
              :disabled="!agentInput.trim() || isLoading"
              class="send-btn"
            >
              <i class="icon" v-if="!isLoading">🚀</i>
              <div class="spinner" v-else></div>
            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, nextTick, onMounted, computed } from "vue";

export default {
  name: "AgentSection",
  props: {
    isActive: {
      type: Boolean,
      default: false,
    },
  },
  setup() {
    const agentInput = ref("");
    const agentMessages = ref([]);
    const agentMessagesRef = ref(null);
    const currentSessionId = ref("");
    const sessionList = ref([]);
    const isLoading = ref(false);
    const isTyping = ref(false);
    const isOnline = ref(true);
    const pendingFeedbacks = ref(new Map()); // 存储待反馈的消息

    // 添加反馈处理方法
    const submitFeedback = async (feedbackId, isEffective, comment = "") => {
      try {
        const response = await fetch(
          "http://127.0.0.1:3210/ai/agent/feedback",
          {
            method: "POST",
            headers: {
              "Content-Type": "application/json",
            },
            body: JSON.stringify({
              feedbackId: feedbackId,
              effective: isEffective,
              comment: comment,
            }),
          }
        );

        if (response.ok) {
          const result = await response.json();
          if (result.success) {
            // 显示反馈结果
            agentMessages.value.push({
              type: "assistant",
              content: `✅ ${result.message}`,
              timestamp: Date.now(),
            });

            // 移除反馈按钮
            pendingFeedbacks.value.delete(feedbackId);
            updateCurrentSessionMessages();
            await scrollToBottom(agentMessagesRef);
          }
        }
      } catch (error) {
        console.error("提交反馈失败:", error);
      }
    };

    // 提取反馈ID的方法
    const extractFeedbackId = (content) => {
      const match = content.match(/\*\*反馈ID\*\*:\s*`([^`]+)`/);
      return match ? match[1] : null;
    };

    // 检查消息是否包含反馈请求
    const hasFeedbackRequest = (content) => {
      return (
        content.includes("这个解决方案对您有帮助吗？") &&
        content.includes("反馈ID")
      );
    };

    // 计算属性
    const currentSession = computed(() => {
      return sessionList.value.find(
        (s) => s.sessionId === currentSessionId.value
      );
    });

    // 生成新的sessionId
    const generateSessionId = () => {
      return (
        "session_" + Date.now() + "_" + Math.random().toString(36).substr(2, 9)
      );
    };

    // 获取会话显示名称
    const getSessionDisplayName = (session) => {
      if (!session || !session.sessionId) return "未知会话";

      // 提取sessionId的后8位作为简短标识
      const shortId =
        session.sessionId.length > 8
          ? session.sessionId.substring(session.sessionId.length - 8)
          : session.sessionId;

      return `对话 ${shortId}`;
    };

    // 格式化时间
    const formatTime = (timestamp) => {
      if (!timestamp) return "";
      const date = new Date(timestamp);
      const now = new Date();
      const diff = now - date;

      if (diff < 60000) return "刚刚";
      if (diff < 3600000) return `${Math.floor(diff / 60000)}分钟前`;
      if (diff < 86400000) return `${Math.floor(diff / 3600000)}小时前`;
      return `${Math.floor(diff / 86400000)}天前`;
    };

    const formatMessageTime = (timestamp) => {
      if (!timestamp) return "";
      return new Date(timestamp).toLocaleTimeString("zh-CN", {
        hour: "2-digit",
        minute: "2-digit",
      });
    };

    // 格式化消息内容
    const formatMessage = (content) => {
      if (!content) return "";

      // 转换markdown风格的格式
      let formatted = content
        .replace(/\*\*(.*?)\*\*/g, "<strong>$1</strong>")
        .replace(/\*(.*?)\*/g, "<em>$1</em>")
        .replace(/```([\s\S]*?)```/g, "<pre><code>$1</code></pre>")
        .replace(/`(.*?)`/g, "<code>$1</code>")
        .replace(/\n/g, "<br>");

      return formatted;
    };

    // 从服务器获取会话列表
    const loadSessionsFromServer = async () => {
      try {
        isLoading.value = true;
        const response = await fetch("http://127.0.0.1:3210/ai/agent/sessions");

        if (response.ok) {
          const result = await response.json();
          if (result.success && result.sessions) {
            sessionList.value = result.sessions;

            // 如果有当前会话ID，加载对应的消息
            if (currentSessionId.value) {
              await loadSessionMessages(currentSessionId.value);
            } else if (sessionList.value.length > 0) {
              currentSessionId.value = sessionList.value[0].sessionId;
              await loadSessionMessages(currentSessionId.value);
            }
          }
        }
      } catch (error) {
        console.error("加载会话列表失败:", error);
        // 如果服务器加载失败，从localStorage加载
        loadSessionsFromStorage();
      } finally {
        isLoading.value = false;
      }
    };

    // 从服务器加载指定会话的消息
    const loadSessionMessages = async (sessionId) => {
      try {
        const response = await fetch(
          `http://127.0.0.1:3210/ai/agent/sessions/${sessionId}/messages`
        );

        if (response.ok) {
          const result = await response.json();
          if (result.success && result.messages) {
            agentMessages.value = result.messages.map((msg) => ({
              type: msg.type === "user" ? "user" : "assistant",
              content: msg.text,
              timestamp: msg.timestamp || Date.now(),
            }));
            await scrollToBottom(agentMessagesRef);
          }
        }
      } catch (error) {
        console.error("加载会话消息失败:", error);
      }
    };

    // 创建新会话
    const createNewSession = async () => {
      try {
        isLoading.value = true;
        const response = await fetch(
          "http://127.0.0.1:3210/ai/agent/sessions",
          {
            method: "POST",
            headers: {
              "Content-Type": "application/json",
            },
            body: JSON.stringify({
              title: "新对话",
            }),
          }
        );

        if (response.ok) {
          const result = await response.json();
          if (result.success && result.sessionId) {
            const newSession = {
              sessionId: result.sessionId,
              title: "新对话",
              messageCount: 0,
              lastUpdated: new Date().toISOString(),
            };

            sessionList.value.unshift(newSession);
            currentSessionId.value = result.sessionId;
            agentMessages.value = [];

            saveSessionsToStorage();
          }
        }
      } catch (error) {
        console.error("创建新会话失败:", error);
        // 降级到本地创建
        const newSessionId = generateSessionId();
        const newSession = {
          sessionId: newSessionId,
          title: "新对话",
          messageCount: 0,
          lastUpdated: new Date().toISOString(),
        };

        sessionList.value.unshift(newSession);
        currentSessionId.value = newSessionId;
        agentMessages.value = [];
        saveSessionsToStorage();
      } finally {
        isLoading.value = false;
      }
    };

    // 切换会话
    const switchSession = async () => {
      await loadSessionMessages(currentSessionId.value);
      saveSessionsToStorage();
    };

    // 删除当前会话
    const deleteCurrentSession = async () => {
      if (sessionList.value.length <= 1 || isLoading.value) return;

      const currentSessionName = getSessionDisplayName(currentSession.value);
      if (!confirm(`确定要删除"${currentSessionName}"吗？此操作不可恢复。`))
        return;

      try {
        isLoading.value = true;
        const response = await fetch(
          `http://127.0.0.1:3210/ai/agent/sessions/${currentSessionId.value}`,
          {
            method: "DELETE",
          }
        );

        if (response.ok) {
          const result = await response.json();
          if (result.success) {
            // 从列表中移除
            const index = sessionList.value.findIndex(
              (s) => s.sessionId === currentSessionId.value
            );
            if (index !== -1) {
              sessionList.value.splice(index, 1);

              // 切换到第一个会话
              if (sessionList.value.length > 0) {
                currentSessionId.value = sessionList.value[0].sessionId;
                await loadSessionMessages(currentSessionId.value);
              }

              saveSessionsToStorage();
            }
          }
        }
      } catch (error) {
        console.error("删除会话失败:", error);
        alert("删除会话失败，请稍后重试");
      } finally {
        isLoading.value = false;
      }
    };

    // 刷新会话列表
    const refreshSessions = async () => {
      await loadSessionsFromServer();
    };

    // 发送建议消息
    const sendSuggestion = (suggestion) => {
      agentInput.value = suggestion;
      sendAgentMessage();
    };

    // 保存会话到localStorage（备份）
    const saveSessionsToStorage = () => {
      localStorage.setItem("chatSessions", JSON.stringify(sessionList.value));
      localStorage.setItem("currentSessionId", currentSessionId.value);
    };

    // 从localStorage加载会话（备份方案）
    const loadSessionsFromStorage = () => {
      const savedSessions = localStorage.getItem("chatSessions");
      const savedCurrentSessionId = localStorage.getItem("currentSessionId");

      if (savedSessions) {
        sessionList.value = JSON.parse(savedSessions);
        if (
          savedCurrentSessionId &&
          sessionList.value.find((s) => s.sessionId === savedCurrentSessionId)
        ) {
          currentSessionId.value = savedCurrentSessionId;
          const currentSession = sessionList.value.find(
            (s) => s.sessionId === currentSessionId.value
          );
          if (currentSession && currentSession.messages) {
            agentMessages.value = currentSession.messages;
          }
        } else if (sessionList.value.length > 0) {
          currentSessionId.value = sessionList.value[0].sessionId;
          if (sessionList.value[0].messages) {
            agentMessages.value = sessionList.value[0].messages;
          }
        }
      } else {
        createNewSession();
      }
    };

    // 更新当前会话的消息
    const updateCurrentSessionMessages = () => {
      const currentSession = sessionList.value.find(
        (s) => s.sessionId === currentSessionId.value
      );
      if (currentSession) {
        currentSession.messages = [...agentMessages.value];
        currentSession.messageCount = agentMessages.value.length;
        currentSession.lastUpdated = new Date().toISOString();
        saveSessionsToStorage();
      }
    };

    const scrollToBottom = async (ref) => {
      await nextTick();
      if (ref.value) {
        ref.value.scrollTop = ref.value.scrollHeight;
      }
    };

    const sendAgentMessage = async () => {
      if (!agentInput.value.trim() || isLoading.value) return;

      const timestamp = Date.now();
      agentMessages.value.push({
        type: "user",
        content: agentInput.value,
        timestamp: timestamp,
      });

      const userInput = agentInput.value;
      agentInput.value = "";
      isLoading.value = true;
      isTyping.value = true;

      await scrollToBottom(agentMessagesRef);

      try {
        const response = await fetch(
          `http://127.0.0.1:3210/ai/agent/process?command=${encodeURIComponent(
            userInput
          )}&sessionId=${encodeURIComponent(currentSessionId.value)}`
        );
        const result = await response.text();

        isTyping.value = false;

        // 检查是否包含反馈请求
        const feedbackId = extractFeedbackId(result);
        if (feedbackId && hasFeedbackRequest(result)) {
          // 存储反馈ID
          pendingFeedbacks.value.set(feedbackId, {
            userQuestion: userInput,
            timestamp: Date.now(),
          });
        }

        agentMessages.value.push({
          type: "assistant",
          content: result,
          timestamp: Date.now(),
          feedbackId: feedbackId, // 添加反馈ID
        });

        updateCurrentSessionMessages();
        await scrollToBottom(agentMessagesRef);
      } catch (error) {
        console.error("发生错误:", error);
        isTyping.value = false;
        agentMessages.value.push({
          type: "assistant",
          content: "抱歉，发生了错误：" + error.message,
          timestamp: Date.now(),
        });
        updateCurrentSessionMessages();
        await scrollToBottom(agentMessagesRef);
      } finally {
        isLoading.value = false;
      }
    };

    // 组件挂载时加载会话
    onMounted(() => {
      loadSessionsFromServer();
    });

    return {
      agentInput,
      agentMessages,
      agentMessagesRef,
      currentSessionId,
      sessionList,
      currentSession,
      isLoading,
      isTyping,
      isOnline,
      sendAgentMessage,
      createNewSession,
      switchSession,
      deleteCurrentSession,
      refreshSessions,
      sendSuggestion,
      formatTime,
      formatMessageTime,
      formatMessage,
      getSessionDisplayName,
      pendingFeedbacks,
      submitFeedback,
      extractFeedbackId,
      hasFeedbackRequest,
    };
  },
};
</script>

<style scoped>
.content-section {
  /* 移除固定高度，使用flex布局 */
  display: flex;
  flex-direction: column;
  height: 100%;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.agent-container {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-height: 100vh;
  overflow: hidden;
}

.agent-header {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border-bottom: 1px solid rgba(0, 0, 0, 0.1);
  padding: 20px 30px;
  box-shadow: 0 2px 20px rgba(0, 0, 0, 0.1);
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.agent-header h2 {
  margin: 0;
  font-size: 24px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 10px;
  color: #2c3e50;
}

.ai-icon {
  font-size: 28px;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%,
  100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.05);
  }
}

.status-indicator {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  padding: 8px 16px;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.8);
  border: 2px solid #e0e0e0;
  font-weight: 500;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #ff4757;
  animation: blink 1.5s infinite;
}

.status-indicator.online .status-dot {
  background: #2ed573;
}

@keyframes blink {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.3;
  }
}

.chat-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: white;
  margin: 20px;
  border-radius: 20px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  min-height: 0; /* 重要：允许flex子项收缩 */
}

.session-header {
  padding: 20px;
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  border-bottom: 1px solid #dee2e6;
}

.session-controls {
  display: flex;
  gap: 15px;
  align-items: center;
  flex-wrap: wrap;
}

.control-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  border: 2px solid transparent;
  border-radius: 12px;
  background: white;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.new-session-btn:hover {
  background: #e3f2fd;
  border-color: #2196f3;
  transform: translateY(-2px);
}

.delete-btn:hover:not(:disabled) {
  background: #ffebee;
  border-color: #f44336;
  transform: translateY(-2px);
}

.refresh-btn:hover:not(:disabled) {
  background: #f3e5f5;
  border-color: #9c27b0;
  transform: translateY(-2px);
}

.control-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

.session-selector {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 5px;
  min-width: 200px;
}

.session-select {
  padding: 12px 16px;
  border: 2px solid #e0e0e0;
  border-radius: 12px;
  font-size: 14px;
  background: white;
  cursor: pointer;
  transition: all 0.3s ease;
}

.session-select:focus {
  outline: none;
  border-color: #2196f3;
  box-shadow: 0 0 0 3px rgba(33, 150, 243, 0.1);
}

.session-info {
  display: flex;
  gap: 15px;
  font-size: 12px;
  color: #666;
}

.icon {
  font-size: 16px;
}

.rotating {
  animation: rotate 1s linear infinite;
}

@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  background: #fafafa;
  min-height: 0; /* 重要：允许滚动 */
}

.welcome-message {
  text-align: center;
  padding: 40px 20px;
}

.welcome-icon {
  font-size: 48px;
  margin-bottom: 20px;
}

.welcome-content h3 {
  color: #2c3e50;
  margin-bottom: 10px;
  font-size: 24px;
  font-weight: 600;
}

.welcome-content p {
  color: #7f8c8d;
  margin-bottom: 30px;
  font-size: 16px;
}

.suggestions {
  display: grid;
  grid-template-columns: 1fr;
  gap: 15px;
  max-width: 500px;
  margin: 0 auto;
}

.suggestion-item {
  padding: 16px 24px;
  background: white;
  border: 2px solid #e0e0e0;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.3s ease;
  font-size: 15px;
  font-weight: 500;
  text-align: left;
}

.suggestion-item:hover {
  background: #e3f2fd;
  border-color: #2196f3;
  transform: translateY(-2px);
  box-shadow: 0 4px 15px rgba(33, 150, 243, 0.2);
}

.message {
  display: flex;
  gap: 12px;
  margin-bottom: 20px;
  animation: fadeInUp 0.5s ease;
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.message.user {
  flex-direction: row-reverse;
}

.message-avatar {
  flex-shrink: 0;
}

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: bold;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.user-avatar {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.assistant-avatar {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
  color: white;
}

.message-bubble {
  max-width: 70%;
  min-width: 100px;
}

.message-content {
  padding: 16px 20px;
  border-radius: 18px;
  word-wrap: break-word;
  line-height: 1.6;
  font-size: 14px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.message.user .message-content {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border-bottom-right-radius: 6px;
}

.message.assistant .message-content {
  background: white;
  color: #2c3e50;
  border: 1px solid #e0e0e0;
  border-bottom-left-radius: 6px;
}

.message-content :deep(strong) {
  font-weight: 600;
}

.message-content :deep(code) {
  background: rgba(0, 0, 0, 0.1);
  padding: 3px 8px;
  border-radius: 6px;
  font-family: "Consolas", "Monaco", monospace;
  font-size: 13px;
}

.message-content :deep(pre) {
  background: #f8f9fa;
  border: 1px solid #e9ecef;
  border-radius: 8px;
  padding: 16px;
  margin: 10px 0;
  overflow-x: auto;
  font-family: "Consolas", "Monaco", monospace;
  font-size: 13px;
}

.message-time {
  font-size: 11px;
  color: #95a5a6;
  margin-top: 6px;
  text-align: right;
}

.message.user .message-time {
  text-align: left;
}

.typing-animation {
  display: flex;
  gap: 4px;
  padding: 16px 20px;
  background: white;
  border-radius: 18px;
  border-bottom-left-radius: 6px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.typing-animation span {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #bdc3c7;
  animation: typing 1.4s infinite ease-in-out;
}

.typing-animation span:nth-child(2) {
  animation-delay: 0.2s;
}

.typing-animation span:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes typing {
  0%,
  80%,
  100% {
    transform: scale(0.8);
    opacity: 0.5;
  }
  40% {
    transform: scale(1);
    opacity: 1;
  }
}

.chat-input {
  padding: 20px;
  background: white;
  border-top: 1px solid #e0e0e0;
}

.input-container {
  display: flex;
  gap: 12px;
  align-items: center;
}

.message-input {
  flex: 1;
  padding: 14px 20px;
  border: 2px solid #e0e0e0;
  border-radius: 25px;
  font-size: 14px;
  outline: none;
  transition: all 0.3s ease;
  background: #f8f9fa;
}

.message-input:focus {
  border-color: #2196f3;
  box-shadow: 0 0 0 3px rgba(33, 150, 243, 0.1);
  background: white;
}

.message-input:disabled {
  background: #f5f5f5;
  cursor: not-allowed;
}

.send-btn {
  width: 50px;
  height: 50px;
  border: none;
  border-radius: 50%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
}

.send-btn:hover:not(:disabled) {
  transform: scale(1.05);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

.send-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

.spinner {
  width: 20px;
  height: 20px;
  border: 2px solid transparent;
  border-top: 2px solid white;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

/* 滚动条样式 */
.chat-messages::-webkit-scrollbar {
  width: 6px;
}

.chat-messages::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 3px;
}

.chat-messages::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 3px;
}

.chat-messages::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .session-controls {
    flex-direction: column;
    align-items: stretch;
  }

  .session-selector {
    min-width: auto;
  }

  .suggestions {
    grid-template-columns: 1fr;
  }

  .message-bubble {
    max-width: 85%;
  }
  .feedback-buttons {
    display: flex;
    gap: 8px;
    margin-top: 12px;
    flex-wrap: wrap;
  }

  .feedback-btn {
    padding: 8px 16px;
    border: none;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    gap: 4px;
  }

  .effective-btn {
    background: #e8f5e8;
    color: #2e7d32;
    border: 1px solid #4caf50;
  }

  .effective-btn:hover {
    background: #4caf50;
    color: white;
    transform: translateY(-1px);
  }

  .ineffective-btn {
    background: #ffebee;
    color: #c62828;
    border: 1px solid #f44336;
  }

  .ineffective-btn:hover {
    background: #f44336;
    color: white;
    transform: translateY(-1px);
  }

  .comment-btn {
    background: #e3f2fd;
    color: #1565c0;
    border: 1px solid #2196f3;
  }

  .comment-btn:hover {
    background: #2196f3;
    color: white;
    transform: translateY(-1px);
  }
}
</style>
